<?php
App::uses('AppController', 'Controller');

class DekanatsController extends AppController {

	public function index() {
		// для установки специальности и курса
		$this->loadModel('Facultet');
		$facultets = $this->Facultet->find('list');
		$this->set(compact('facultets'));
		$this->loadModel('Profession');
		$selectedprofessions = $this->Profession->find('list');
		$this->set(compact('selectedprofessions'));
		$this->loadModel('Course');
		$selectedcourses = $this->Course->find('list');
		$this->set(compact('selectedcourses'));
		$this->loadModel('Studentgroup');
		$selectedgroups = $this->Studentgroup->find('list');
		$this->set(compact('selectedgroups'));

		// вывод установки если есть
		// иницилизация переменных настроек
		$selectedprofession = '';
		$selectedcourse = '';
		$selectedgroup = '';
		if ($this->Session->check('Settings')){
			$settings = $this->Session->read('Settings');
			$selectedprofession = $settings['Settings']['selectedprofession'];
			$selectedcourse = $settings['Settings']['selectedcourse'];
			$selectedgroup = $settings['Settings']['selectedgroup'];
		}
		$this->set(compact('selectedprofession', $selectedprofession));
		$this->set(compact('selectedcourse', $selectedcourse));
		$this->set(compact('selectedgroup', $selectedgroup));		
		// для формирование отображения списка
		$professions = $this->Profession->find('list');
		$this->set(compact('professions'));
	}

	function settings_set() {
		if (!empty($this->data)) {
			$this->Session->delete('Settings');
		//	$selectedfacultet = '';
			$selectedprofession = '';
			$selectedcourse = '';
			$selectedgroup = '';
	//		if ($this->data['Dekanat']['facultet_id']) {
	//			$selectedfacultet = $this->data['Dekanat']['facultet_id'];
	//		}
			if ($this->data['Dekanat']['profession_id']) {
				$selectedprofession = $this->data['Dekanat']['profession_id'];
			}
			if ($this->data['Dekanat']['course_id']) {
				$selectedcourse = $this->data['Dekanat']['course_id'];
			}
			if ($this->data['Dekanat']['studentgroup_id']) {
				$selectedgroup = $this->data['Dekanat']['studentgroup_id'];
			}
			$settings = array(		'selectedprofession' => $selectedprofession,
									'selectedcourse' => $selectedcourse,
									'selectedgroup' => $selectedgroup);
			$sessionsettings = array ('Settings' => $settings );
			$this->Session->write('Settings', $sessionsettings);
		}
		//	debug($this->data['Student']);
		$this->redirect(array('action' => 'index'));
	}
	
	function settings_unset() {
		if (!empty($this->data)) {
			$this->Session->delete('Settings');
		}
		$this->redirect(array('action' => 'index'));
	}
	public function update_course () {
		if(!empty($this->data['Dekanat']['profession_id'])) {
			$profession_id = $this->data['Dekanat']['profession_id'];
			$this->loadModel('Course');
			$options = $this->Course->find('list'  , array('conditions' => array ('Course.profession_id' => $profession_id)));
			$this->set(compact('options'));
			$this->render('update_course', 'blank');
		}
	}	
	
	public function update_studentgroup () {
		if(!empty($this->data['Dekanat']['course_id'])) {
			$course_id = $this->data['Dekanat']['course_id'];
			$this->loadModel('Studentgroup');
			$options = $this->Studentgroup->find('list'  , array('conditions' => array ('Studentgroup.course_id' => $course_id)));
			$this->set(compact('options'));
			$this->render('update_studentgroup', 'blank');
		}
	}	
	
	public function liststudents(){
		// определяем формат результата
		$format = $this->data['Student']['format'];
		
		$sessionsettings = '';
		$selectedfacultet = '';
		$selectedprofession = '';
		$selectedcourse = '';
		$selectedgroup = '';
		if ($this->Session->check('Settings')){
			$sessionsettings = $this->Session->read('Settings');
			$selectedfacultet = $sessionsettings['Settings']['selectedfacultet'];
			$selectedprofession = $sessionsettings['Settings']['selectedprofession'];
			$selectedcourse = $sessionsettings['Settings']['selectedcourse'];
			$selectedgroup = $sessionsettings['Settings']['selectedgroup'];
		} else {
			// если настрое отображения нет берем настройки вывода факультетов из facultetssettings
			$facultetsettings = $this->Session->read('FacultetsSettings');
			//$selectedfacultet
		}
		$this->set(compact('selectedfacultet', $selectedfacultet));
		$this->set(compact('selectedprofession', $selectedprofession));
		$this->set(compact('selectedcourse', $selectedcourse));
		$this->set(compact('selectedgroup', $selectedgroup));
	}
}
?>